package com.leetcode;


import java.util.Calendar;
import java.util.Date;

/**
 * 2409. 统计共同度过的日子数 (简单)
 * Alice 和 Bob 计划分别去罗马开会。
 * 给你四个字符串 arriveAlice ，leaveAlice ，arriveBob 和 leaveBob 。Alice
 * 会在日期 arriveAlice 到 leaveAlice 之间在城市里（日期为闭区间），而 Bob
 * 在日期 arriveBob 到 leaveBob 之间在城市里（日期为闭区间）。每个字符串都包含 5 个字符，格式为 "MM-DD" ，对应着一个日期的月和日。
 * 请你返回 Alice和 Bob 同时在罗马的天数。
 * 你可以假设所有日期都在 同一个 自然年，而且 不是 闰年。每个月份的天数分别为：[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 。
 */
public class LeetCode2409 {

    public static void main(String[] arg0) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.YEAR, 1);
        System.out.println("当前时间加1年 = " + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");

        calendar.roll(Calendar.MONTH, -5);
        System.out.println("再减五个月 = " + calendar.get(Calendar.YEAR) + "年" + (calendar.get(Calendar.MONTH) + 1) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");

        String arriveAlice = "08-15", leaveAlice = "08-18", arriveBob = "08-16", leaveBob = "08-19";
        System.out.println("统计共同度过的日子数 = " + countDaysTogether(arriveAlice, leaveAlice, arriveBob
                , leaveBob));
    }

    public static int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob,
                                        String leaveBob) {
        // 同一年，所以只比较月份和天
        int arriveAliceMonth = Integer.parseInt(arriveAlice.substring(0, arriveAlice.indexOf("-")));
        int arriveAliceDay = Integer.parseInt(arriveAlice.substring(arriveAlice.indexOf("-") + 1));

        int leaveAliceMonth = Integer.parseInt(leaveAlice.substring(0, leaveAlice.indexOf("-")));
        int leaveAliceDay = Integer.parseInt(leaveAlice.substring(leaveAlice.indexOf("-") + 1));

        int arriveBobMonth = Integer.parseInt(arriveBob.substring(0, arriveBob.indexOf("-")));
        int arriveBobDay = Integer.parseInt(arriveBob.substring(arriveBob.indexOf("-") + 1));

        int leaveBobMonth = Integer.parseInt(leaveBob.substring(0, leaveBob.indexOf("-")));
        int leaveBobDay = Integer.parseInt(leaveBob.substring(leaveBob.indexOf("-") + 1));

        // 先写复杂判断
        if (arriveAliceMonth > leaveBobMonth || (arriveAliceMonth == leaveBobMonth && arriveAliceDay > leaveBobDay)) {
            return 0; // Alice 在 Bob 离开后到达
        } else if (arriveBobMonth > leaveAliceMonth || (arriveBobMonth == leaveAliceMonth && arriveBobDay > leaveAliceDay)) {
            return 0; // Bob 在 Alice 离开后到达
        } else { // 有交叉，而且交叉的月份不相同，取早的离开日期 和 迟的到达日期，计算中间的天数即可
            int leaveMonth, leaveDay;
            if (leaveAliceMonth == leaveBobMonth) {
                leaveMonth = leaveAliceMonth;
                leaveDay = Math.min(leaveAliceDay, leaveBobDay);
            } else if (leaveAliceMonth < leaveBobMonth) {
                leaveMonth = leaveAliceMonth;
                leaveDay = leaveAliceDay;
            } else {
                leaveMonth = leaveBobMonth;
                leaveDay = leaveBobDay;
            }

            int arriveMonth, arriveDay;
            if (arriveAliceMonth == arriveBobMonth) {
                arriveMonth = arriveAliceMonth;
                arriveDay = Math.max(arriveAliceDay, arriveBobDay);
            } else if (arriveAliceMonth > arriveBobMonth) {
                arriveMonth = arriveAliceMonth;
                arriveDay = arriveAliceDay;
            } else {
                arriveMonth = arriveBobMonth;
                arriveDay = arriveBobDay;
            }

            if (leaveMonth == arriveMonth) {
                return leaveDay - arriveDay + 1;
            }

            int dis = 0;
            for (int i = leaveMonth; i >= arriveMonth; i--) {
                if (i == leaveMonth) {
                    dis += leaveDay;
                } else if (i == arriveMonth) {
                    dis += maxDay(i) - arriveDay + 1;
                } else {
                    dis += maxDay(i);
                }
            }
            return dis;
        }
    }

    // 获取当前月份的最大天数
    public static int maxDay(int currentMonth) {
        if (currentMonth == 2) {
            return 28;
        } else if (currentMonth == 4 || currentMonth == 6 || currentMonth == 9 || currentMonth == 11) {
            return 30;
        } else {
            return 31;
        }
    }


}
